package com.zmn.oms.zmn.business.impl.track;

import com.zmn.oms.business.interfaces.mq.MqProviderService;
import org.apache.dubbo.config.annotation.Reference;
import com.zmn.oms.business.interfaces.track.OrderTrackBService;
import com.zmn.oms.business.interfaces.xno.OrderXnoBindService;
import com.zmn.oms.model.dto.track.*;
import com.zmn.oms.model.entity.masterworking.MasterWorking;
import com.zmn.oms.model.entity.track.OrderTrackQuery;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.masterworking.MasterWorkingService;
import com.zmn.oms.services.interfaces.track.OrderTrackService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.services.interfaces.work.masterwork.MasterWorkService;
import com.zmn.oms.zmn.business.interfaces.track.ZsOrderTrackBService;

import io.shardingsphere.core.keygen.DefaultKeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 类描述：跟单实现类
 *
 * @author heciqi
 * @date 2018/11/27 14:24
 */
@Service
@Deprecated
public abstract class ZsOrderTrackBServiceImpl implements ZsOrderTrackBService {

	@Autowired
	private OrderTrackBService orderTrackBService;

	@Autowired
	private OrderTrackService orderTrackService;

	@Autowired
	private DefaultKeyGenerator defaultKeyGenerator;

	@Autowired
	private MasterWorkService masterWorkService;

	@Autowired
	private MasterWorkingService masterWorkingService;

	@Autowired
	private OrderXnoBindService orderXnoBindService;

	@Autowired
	private OrderWorkService orderWorkService;

	@Autowired
	private OrderDetailService orderDetailService;

	//@Reference(version = com.zmn.base.engineer.common.constants.DubboConsts.INTERFACE_VERSION, check = false)
	//private EngineerTouchModifyRemoteService engineerTouchModifyRemoteService;

	@Autowired
	private MqProviderService mqProviderService;

	/**
	 * 数量查询
	 *
	 * @param query
	 * @return
	 */
	@Override
	public Integer countByQuery(OrderTrackQuery query) {
		return orderTrackService.countByQuery(query);
	}

	/**
	 * 新增工程师跟单
	 *
	 * @param addOrderTrackDTO
	 * @return
	 */
	@Override
	public void insertMasterTrack(AddOrderTrackDTO addOrderTrackDTO) {
		orderTrackBService.insertMasterTrack(addOrderTrackDTO);
	}

	/**
	 * 新增后台用户跟单
	 *
	 * @param addOrderTrackDTO
	 * @return
	 */
	@Override
	public void insertUserTrack(AddOrderTrackDTO addOrderTrackDTO) {
		orderTrackBService.insertUserTrack(addOrderTrackDTO);
	}

	/**
	 * 跟单系统更改订单系统状态
	 *
	 * @param workTrackDTO
	 * @return void
	 * @author wangxiaokun
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void modifyOrderWorkStatus(WorkTrackDTO workTrackDTO){
		Long workId = workTrackDTO.getWorkId();

		// 同步进行中的工程师工单表
		masterWorkingService.updateMasterWorkingStatus(workId, null, null, workTrackDTO.getWaitCancel());

		MasterWorking masterWorkingByWorkId = masterWorkingService.findMasterWorkingByWorkId(workId);
		if (masterWorkingByWorkId != null) {
			// 刷新工程师工单列表版本号
			//engineerTouchModifyRemoteService.saveEngineerWorkVersion(masterWorkingByWorkId.getMasterId(), null);
			mqProviderService.sendEngineerWorkVersion(masterWorkingByWorkId.getMasterId(), null);
		}

		// 暂时不做
		/*OrderWork dbOrderWork = orderWorkService.findOrderWorkByKey(orderId, workId);

		// 取消跟单：正在进行中解绑小号，完结跟单重新绑定小号
		if (Objects.equals(workTrackDTO.getTrackStatus(), OrderConsts.ORDER_TRACK_STATUS_DOING)){
			OrderDetail orderDetail = orderDetailService.findOrderDetailByKey(orderId);
			orderXnoBindService.unBind(dbOrderWork, orderDetail);
		} else if (Objects.equals(workTrackDTO.getTrackStatus(), OrderConsts.ORDER_TRACK_STATUS_DONE)){
			orderXnoBindService.bind(dbOrderWork);
		}*/

	}

	/**
	 * 修改跟单结果
	 *
	 * @param completeOrderTrackDTO
	 * @return
	 */
	@Override
	public void updateResultTrack(CompleteOrderTrackDTO completeOrderTrackDTO) {
		orderTrackBService.updateResultTrack(completeOrderTrackDTO);
	}
}
